package com.qyj.guiyangdemo.controller;

import com.qyj.guiyangdemo.entity.*;
import com.qyj.guiyangdemo.service.OrderService;
import com.qyj.guiyangdemo.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/save")
    public Result save(@RequestBody WorkOrder workOrder) {
        if (workOrder == null) {
            return ResultUtil.is_null();
        }
        if (workOrder.getOrderNo() == null || workOrder.getOrderType() == null ||
                workOrder.getTitle() == null || workOrder.getContent() == null ||
                workOrder.getTitle().equals("") || workOrder.getContent().equals("")) {
            return ResultUtil.is_null();
        }
        Result byOrderNo = orderService.getByOrderNo(workOrder.getOrderNo());
        if (byOrderNo != null) {
            return byOrderNo;
        }
        System.out.println("66666666 = " + workOrder);
        boolean save = orderService.save(workOrder);
        if (save) {
            return ResultUtil.ok("保存成功");
        }
        return ResultUtil.error("保存失败");
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody WorkOrder workOrder) {
        if (workOrder == null) {
            return ResultUtil.is_null();
        }
        if (workOrder.getId() == null) {
            return ResultUtil.error("id不能为null");
        }
        Long id = workOrder.getId();
        boolean t = orderService.removeById(id);
        if (t) {
            return ResultUtil.ok("删除成功");
        }
        return ResultUtil.error("删除失败");
    }

    @PostMapping("/update")
    public Result update(@RequestBody WorkOrder workOrder) {
        if (workOrder == null) {
            return ResultUtil.is_null();
        }
        if (workOrder.getId() == null) {
            return ResultUtil.error("id不能为null");
        }
        if (workOrder.getOrderNo() == null || workOrder.getOrderType() == null ||
                workOrder.getTitle() == null || workOrder.getContent() == null ||
                workOrder.getTitle().equals("") || workOrder.getContent().equals("")) {
            return ResultUtil.is_null();
        }
        boolean t = orderService.updateById(workOrder);
        if (t) {
            return ResultUtil.ok("修改成功");
        }
        return ResultUtil.error("修改失败");
    }

    @PostMapping("/search")
    public ArrayList<WorkOrder> search(@RequestBody PageNum pageNum) {
        if (pageNum == null) {
            return null;
        }
        if (pageNum.getPage() == null) {
            pageNum.setPage(0);
        } else if (pageNum.getPage() < 0) {
            pageNum.setPage(0);
        }
        if (pageNum.getNum() == null) {
            pageNum.setNum(20);
        } else if (pageNum.getNum() < 0) {
            pageNum.setNum(20);
        }
        return orderService.search(pageNum);
    }

    @PostMapping("/fenpai")
    public Result fenpai(@RequestBody Fenpai reqfen) {
        if (reqfen == null) {
            return ResultUtil.is_null();
        }
        Long orderId = reqfen.getOrderId();
        Long deptId = reqfen.getDeptId();
        String deptName = reqfen.getDeptName();
        if (orderId == null || deptId == null || deptName == null || deptName.equals("")) {
            return ResultUtil.error("工单id(orderId)，处理部门id(deptId)，处理部门名称(deptName) 不能为空");
        }
        if (orderService.setdeptAndTime(orderId, deptId)) {
            return ResultUtil.ok("分派成功");
        }
        return ResultUtil.error("分派失败");
    }

    @GetMapping("/res")
    public ArrayList<NumInfo> getNumtt(@RequestParam(value = "param", defaultValue = "1") Integer t1, @RequestParam(value = "month", defaultValue = "1") Integer month) {
// todo
//        4.查询7月每天的工单总量、超期率 （三选二） param=1
//        5.查询7月每个部门的工单总量、超期率。（三选二） param=2
//        6.查询7月每个工单类型的工单总量、超期率。（三选二） param=3
        switch (t1) {
            case 1:
                return orderService.getByParamOne(month);
            case 2:
                return orderService.getByParamTwo(month);
            case 3:
                return orderService.getByParamThree(month);
            default:
                return null;
        }
    }
}
